#include <algorithm>
#include <iostream>
#include <vector>

/**
 * PAT A 1015 Reversible Primes
 */

int reverse(int num, int radix) {
    int result = 0;
    while (num != 0) {
        int digit = num % radix;
        result = result * radix + digit;
        num /= radix;
    }
    return result;
}

int main() {
    // Read cases and compute their reversed counterparts
    std::vector<std::pair<int, int> > cases;
    int maxN = 0;
    while (true) {
        int num, radix;
        scanf("%d %d", &num, &radix);
        if (num < 0) break;
        int rev = reverse(num, radix);
        cases.push_back(std::make_pair(num, rev));
        maxN = std::max(maxN, num);
        maxN = std::max(maxN, rev);
    }

    // Sieve primes
    std::vector<bool> isPrime(maxN + 1, true);
    isPrime[1] = false;
    for (int i = 2; i <= maxN; i++) {
        if (!isPrime[i]) continue;
        for (int j = i * i; j <= maxN; j += i) isPrime[j] = false;
    }

    // Output result
    for (int i = 0; i < cases.size(); i++)
        std::cout << (isPrime[cases[i].first] && isPrime[cases[i].second]
                          ? "Yes"
                          : "No")
                  << '\n';

    return 0;
}